package com.itheima.controller;

import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.resources.UploadUtils;
import io.swagger.annotations.Api;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * 生成接口注解
 * @Api
 * @ApiOperation
 * @ApiImplicitParams
 * @ApiImplicitParam
 */
@RestController
@Api(tags = "套餐管理开发")  // 作用在类上
public class SetmealController {
//
    @Reference
    private SetmealService setmealService;

    //分页查询
    @PostMapping("setmeal/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){
        return new Result(setmealService.findPage(queryPageBean));
    }


    //新建套餐提交
    @PostMapping("setmeal/add")
    public Result add(@RequestBody SetmealDTO setmealDTO) {
        return new Result(setmealService.saveSetmealInfo(setmealDTO));
    }
//
//    //逻辑删除
//    @DeleteMapping("checkgroup/delectCheckGroupById/{id}")
//    public Result delectCheckGroupById(@PathVariable("id") int id){
//        return new Result(checkGroupService.updateCheckGroupById(id));
//    }

    //文件下上传
    @PostMapping("setmeal/upload")
    //@RequestParam("imgFile")必须 el-upload 标签 name="imgFile"一直才能接受到上传对象
    public Result upload(@RequestParam("imgFile") MultipartFile file){
        System.out.println("文件上传");
        try {
            //获取上传文件对象 MultipartFile file 的原始名称
            String originalFilename = file.getOriginalFilename();
            //uuid生成新的文件名
            String uuidfileName = UploadUtils.generateRandonFileName(originalFilename);
            //上传到阿里云sdk oss
            AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(), uuidfileName);
            //图片信息保存一份到Redis的set集合中
            RedisUtil.addToSet(RedisConstant.ALL_SETMEAL_PIC_SET, uuidfileName);
            //图片信息保存一份到Redis的Value中并设置超时时间60秒，超时就自动删除
            RedisUtil.set(RedisConstant.SINGLE_PIC+uuidfileName,uuidfileName,1, TimeUnit.MINUTES);
            System.out.println("上传成功");
            //返回新的文件名
            return new Result(uuidfileName);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }
}








